package com.rdgzs.framework.utilsTools;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* ###################################################
* 创建日期：2012-12-27 22:58:45
* 开发人员：李广彬
* 功能描述：生成验证码图片和相应的随机数 和CaptchaServlet 一块使用
* 修改日志： 
* ###################################################
*/
public class CaptchaBuilder {
    private static final long serialVersionUID = 1L;
    public static final String RAND_STRING = "0123456789abcdefghijklmnopqrstuvwxyz";
    private BufferedImage image = null;// 生成的图像文件
    private StringBuffer randStr = null;// 生成图像文件的字符码
    private int singleWidth = 12;// 一个验证字符所占的图片宽度
    private int height = 26;// 图片高度
    private int count = 4;// 随机码的字符个数
    private int offset = 3;// 图片前后的偏移量
    
    

    public static CaptchaBuilder getInstance() {
        return new CaptchaBuilder();
    }

    /**
     * 生成随机字码图片和字符串
     * @param out ServletOutputStream
     * @param sessionKey 
     * @return
     * @throws Exception
     */
    public CaptchaBuilder createRandCode() {
        int width=getWidth();
        randStr = new StringBuffer("");
        Random random = new Random();
        System.setProperty("java.awt.headless", "true");
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.fillRect(0, 0, width, height);// 设定边框
        g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
        g.setColor(getRandColor(111, 133, random));
        // 产生随机线
        for (int i = 0; i < 11; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(13);
            int yl = random.nextInt(15);
            g.drawLine(x, y, x + xl, y + yl);
        }
        // 产生随机点
        g.setColor(getRandColor(130, 150, random));
        for (int i = 0; i < count; i++) {// 产生count个随机数
            String rand = String.valueOf(getRandomString(random.nextInt(10)));
            g.setFont(getsFont(random));
            g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
            g.translate(0, random.nextInt(8)-4);
            g.drawString(rand, singleWidth * i+offset, height-4);
            randStr.append(rand);
            System.out.println((random.nextInt(8)-4));
            System.out.println((random.nextInt(8)-4));
        }
        g.dispose();
        //ImageIO.write(image, "JPEG", out);
        return this;
    }

    /**
     * 得到
     * @param num
     * @return
     */
    public synchronized String getRandomString(int num) {
        return String.valueOf(RAND_STRING.charAt(num));
    }

    /**
     * 随机取得一个字体
     * @param Random random 随机数
     * @return Font 返回一个新字体
     */
    private Font getsFont(Random random) {
        return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
    }

    /**
     * 返回一个随机颜色
     * @param int fc 随机数
     * @param int bc 随机数
     * @param Random random 随机数
     * @return Color 返回一个新颜色
     */
    private Color getRandColor(int fc, int bc, Random random) {
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc - 6);
        int g = fc + random.nextInt(bc - fc - 4);
        int b = fc + random.nextInt(bc - fc - 8);
        return new Color(r, g, b);
    }
    /**
     * 根据字符个数计算图片宽度
     * @return
     */
    public int getWidth() {
        return singleWidth*count+offset*2;
    }

    public BufferedImage getImage() {
        return image;
    }

    public String getRandStr() {
        return randStr.toString();
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public void setRandStr(StringBuffer randStr) {
        this.randStr = randStr;
    }

    public int getSingleWidth() {
        return singleWidth;
    }

    public void setSingleWidth(int singleWidth) {
        this.singleWidth = singleWidth;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
    
    
    
}
